package com.otp.controller;

import com.otp.pojo.dto.PaperSubmissionDto;
import com.otp.pojo.entity.PaperSubmission;
import com.otp.pojo.request.paper.*;
import com.otp.util.ResponseMessage;
import com.otp.service.PaperSubmissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/submission")
public class PaperSubmissionController {

    @Autowired
    private PaperSubmissionService paperSubmissionService;

    @PostMapping("/grade")
    public ResponseMessage<PaperSubmissionDto> grade(@RequestBody PaperSubmissionGradeRequest request) {
        return ResponseMessage.success(paperSubmissionService.grade(request)
                .convertToDto());
    }

    @GetMapping
    public ResponseMessage<List<PaperSubmissionDto>> listSubmissions() {
        return ResponseMessage.success(paperSubmissionService.listSubmissions()
                .stream()
                .map(PaperSubmission::convertToDto)
                .toList());
    }

    @GetMapping("/submission/{submissionId}")
    public ResponseMessage<PaperSubmissionDto> getSubmissionById(@PathVariable Long submissionId) {
        return ResponseMessage.success(paperSubmissionService.getSubmissionById(submissionId)
                .convertToDto());
    }

    @GetMapping("/paper/{paperId}")
    public ResponseMessage<List<PaperSubmissionDto>> listSubmissionsByPaperId(@PathVariable Long paperId) {
        return ResponseMessage.success(paperSubmissionService.listSubmissionsByPaperId(paperId)
                .stream()
                .map(PaperSubmission::convertToDto)
                .toList());
    }

    @GetMapping("/student/{studentId}")
    public ResponseMessage<List<PaperSubmissionDto>> listSubmissionsByStudentId(@PathVariable Long studentId) {
        return ResponseMessage.success(paperSubmissionService.listSubmissionsByStudentId(studentId)
                .stream()
                .map(PaperSubmission::convertToDto)
                .toList());
    }

    @GetMapping("/status/{status}")
    public ResponseMessage<List<PaperSubmissionDto>> listSubmissionsByStatus(@PathVariable String status) {
        return ResponseMessage.success(paperSubmissionService.listSubmissionsByStatus(status)
                .stream()
                .map(PaperSubmission::convertToDto)
                .toList());
    }
}
